package com.unhz.taigu.service.impl;

import com.unhz.generator.dao.OrderMapper;
import com.unhz.generator.dao.SysRolePermissionMapper;
import com.unhz.generator.enums.EnumOderStatus;
import com.unhz.generator.enums.EnumValid;
import com.unhz.generator.pojo.Order;
import com.unhz.generator.pojo.SysRolePermission;
import com.unhz.generator.pojo.User;
import com.unhz.generator.utils.IsNumeric;
import com.unhz.generator.utils.JsonResult;
import com.unhz.generator.utils.Page;
import com.unhz.generator.vo.OrderVo;
import com.unhz.generator.vo.UserVo;
import com.unhz.taigu.service.OrderService;
import com.unhz.taigu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 王林恩
 * @Description:
 * @Date: Created in 上午 9:11 2018/1/29 0029
 * @Modified By:
 */
@Service
public class OrderServiceImpl implements OrderService {
    static final int PAGENUM = 1;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private SysRolePermissionMapper sysRolePermissionMapper;

    /**
     * 分页查询
     * @param pageNo 页数
     * @param pageSize
     * @param keywords 关键词
     * @return
     */
    @Override
    public Page<Order> selectPageOrder(String pageNo, Integer pageSize, String keywords,Integer adminId,Integer roleId) {
        int pageNum = PAGENUM;
        boolean numok = IsNumeric.isNumeric(pageNo);
        if(numok){
            if(pageNo !=null && !"".equals(pageNo)){
                pageNum = Integer.parseInt((pageNo));  // 页数
            }
        }
        //查询roleId中是否存在permissionId对应值的权限
        int count = sysRolePermissionMapper.selectPermissionIdByRoleIdOrder(roleId);
        //获取记录总条数
        int totalRecords = 0;
        if(1==count){
            totalRecords = orderMapper.getTotalRecordsByKeywords(keywords);   //获取记录总条数
        }else{
            totalRecords = orderMapper.selectSomeTotalRecordsByKeywords(adminId,keywords);
        }
        Page page = new Page(pageNum,totalRecords);
        page.setPageSize(pageSize);  //每页显示条数
        Map<String ,Object> map = new HashMap<String,Object>();
        map.put("startIndex",page.getStartIndex());
        map.put("pageSize",page.getPageSize());
        map.put("keywords",keywords);
        map.put("adminId",adminId);
        //获取总记录数
        List<OrderVo> records = new ArrayList<OrderVo>();
        if(1==count){
            records = orderMapper.selectOrderByAdmin(map);  //获取记录
        }else{
            records = orderMapper.selectOrderByAdminId(map);
        }
        page.setRecords(records);
        String uri = "/order/findAllOrder?keywords="+keywords+"&roleId="+roleId+"&adminId="+adminId;
        page.setUri(uri);
        return page;
    }

    /**
     * 删除订单
     * @param id
     * @return
     */
    @Override
    public int deleteOrderById(Integer id) {
        int res = orderMapper.deleteByPrimaryKey(id);
        return res;
    }

    @Override
    public Order selectOrderById(Integer orderId) {
        return orderMapper.selectOrderById(orderId);
    }

    @Override
    public OrderVo findOrderById(Integer orderId) {
        OrderVo order = orderMapper.findOrderById(orderId);
        return order;
    }

    @Override
    public JsonResult addOrder(Order order) {
        int res = orderMapper.insertSelective(order);
        if(res==0){
            return JsonResult.failure("添加失败");
        }
        return JsonResult.success("添加成功");
    }

    @Override
    public int updateOrder(Order order) {
        int res = orderMapper.updateByPrimaryKeySelective(order);
        return res;
    }

    @Override
    public String updateIsenable(Integer OrderId, Integer isenable, String adminName) {
        //修改用户的禁用启用状态
        Order order = new Order();
        order.setId(OrderId);
        order.setStatus(EnumOderStatus.DOCKING.getIndex());
        order.setUpdateBy(adminName);
        int count = orderMapper.updateByPrimaryKeySelective(order);
        if( count > 0){
            return "success";
        }
        return "failed";
    }

    @Override
    public String updateExamine(Integer OrderId, Integer examine, String adminName) {
        //修改用户的禁用启用状态
        Order order = orderMapper.selectByPrimaryKey(OrderId);
        Integer valid = order.getValid();
        if(examine == EnumValid.FIRST_ENABLED.getIndex()){
            order.setBusinessStatus(EnumOderStatus.DOCKING.getIndex());
            switch (valid){
                case 0:
                    valid = EnumValid.ENABLED.getIndex();
                    break;
                case 1:
                    valid = EnumValid.DISABLE.getIndex();
                    break;
                case 2:
                    valid = EnumValid.ENABLED.getIndex();
                    break;
            }
        }
        order.setValid(valid);
        order.setStatus(examine);
        order.setCheckBy(adminName);
        int count = orderMapper.updateByPrimaryKeySelective(order);
        if( count > 0){
            return "success";
        }
        return "failed";
    }

    @Override
    public String finishDock(Integer orderId) {
        int count =  orderMapper.finishDock(orderId);
        if( count > 0){
            return "success";
        }
        return "failed";
    }

    @Override
    public void keyPass(Order order) {
        orderMapper.keyPass(order);
    }

    @Override
    public int confirmContract(Integer orderId) {
        return orderMapper.confirmContract(orderId);
    }

    @Override
    public List<Order> selectOrder(Integer userId) {
        return orderMapper.selectOrder(userId);
    }

}
